import React, { Component } from "react";
import './index.css'

export default class Item extends Component{
    //鼠标移入移出状态
    state = {mouse:false}
    //鼠标移入事件回调
    handleMouse=(flag)=>{
        return()=>{
            // console.log(flag);
            this.setState({mouse:flag});
        }
    }
    //处理勾选事件
    handleCheck=(id)=>{
        return(event)=>{
            // console.log(id,event.targe.checked);
            this.props.updateTodo(id,event.target.checked);
        }
    }

    //删除item
    handleDelete=(id)=>{
        return()=>{
            if(window.confirm('确定删除吗')) {
                console.log('删除',id);
                this.props.deleteTodo(id);
            }
        }
    }
    render(){
        const {id,name,done} = this.props
        const{mouse} = this.state
        return(
            <li style={{backgroundColor:mouse ? '#ddd' : 'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleCheck(id)}/>
                    <span>{name}</span>
                </label>
                <button onClick={this.handleDelete(id)} className="btn btn-danger" style={{display:mouse ? 'block' : 'none'}}>删除</button>
            </li>
        )
    }
}